package com.luocj.looook.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.luocj.looook.MainActivity;
import com.luocj.looook.R;
import com.luocj.looook.base.BaseActivity;
import com.luocj.looook.utils.Constant;
import com.luocj.looook.utils.SPUtils;

public class SplashActivity extends BaseActivity {

    private static final String TAG = SplashActivity.class.getSimpleName();
    private int TIME = 2;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressLint("SetTextI18n")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    mTime.setText(TIME + "s");
                    TIME--;
                    if (TIME >= 0) {
                        mHandler.sendMessageDelayed(mHandler.obtainMessage(1), 1000);
                    } else {

                        boolean isFirstIn = SPUtils.getBoolean(SplashActivity.this, Constant.IS_FIRST_IN, true);
                        if (isFirstIn) {
                            startActivity(new Intent(SplashActivity.this, IntroActivity.class));
                        } else {
                            startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        }

                        finish();
                    }

                    break;
                default:

                    break;
            }
        }
    };
    private AppCompatTextView mTime;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {
        mTime = findViewById(R.id.tv_splash_time);
        mTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    @Override
    protected void initData() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mHandler.sendMessage(mHandler.obtainMessage(1));
            }
        }, 0);
    }
}
